const express = require("express");
const passport = require("passport");
const router = express.Router();

const db = require("../config/db");
const sql = require("../config/sql").details;


// 查询所有内容
router.get("/", (req, res) => {
  db.query(sql.selectDetails, (err, result) => {
    if (err) throw err;
    res.send({
      status: 200,
      data: result
    });
  });
});

// 添加内容
router.post("/add", passport.authenticate('jwt', { session: false }), (req, res) => {
  const data = {
    title: req.body.title,
    content: req.body.content,
    userId: req.body.userId,
    createTime: new Date()
  };
  db.query(sql.detailsStorage, (err, result) => {
    if (err) throw err;
    res.send({
      status: 200,
      data: "success"
    });
  });
});

// 根据id删除内容
router.delete("/delete/:id", passport.authenticate('jwt', { session: false }), (req, res) => {
  const id = req.params.id;
  db.query(sql.deleteContent + id, (err, data) => {
    if (err) throw err;

    res.send({
      status: 200,
      data: "删除成功"
    });
  });
});

module.exports = router;